package com.gsk.europepharma.salesReport.struts.actions;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import net.sf.json.JSONObject;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.hibernate.Hibernate;
import org.hibernate.Query;
import org.hibernate.Session;

import com.gsk.europepharma.salesReport.hibernate.tbSpBoSRSalesUgp;
import com.gsk.europepharma.salesReport.hibernate.dao.tbSpBoSRSectoDAO;
import com.gsk.europepharma.salesReport.utils.UtilsSalesReport;

/**
 * Gestion des donnees pour le graphe DeltaPM.
 */
public class DataDeltaPm extends Action {
	
	public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
		
		// Variables de session
		HttpSession sessionObject = request.getSession();
		Integer cs_adm_pdt = (Integer) sessionObject.getAttribute("cs_adm_pdt");
		Integer cs_adm_rgpt = (Integer) sessionObject.getAttribute("cs_adm_rgpt");
		if (request.getParameter("product") != null) {
			cs_adm_rgpt = Integer.parseInt(request.getParameter("product"));
		}
		Integer cs_adm_prstn = (Integer) sessionObject.getAttribute("cs_adm_prstn");
		Integer cs_network = (Integer) sessionObject.getAttribute("cs_network");
		Integer cs_month = (Integer) sessionObject.getAttribute("cs_month");
		// Fin de session 
		
		String idGeog = request.getParameter("idGeog");
		Integer idGeogInteger = Integer.parseInt(idGeog);
		
		String nivGeog = request.getParameter("nivGeog");
		
		String source = request.getParameter("source");
		Integer CS_SRC = Integer.parseInt(source);
		String unite = request.getParameter("unite");
		String periode = request.getParameter("periode");
		
		String france = request.getParameter("france");
		String zone = request.getParameter("zone");
		String region = request.getParameter("region");
		
		String indicateur = request.getParameter("indicateur");
		
		String quota = request.getParameter("quota"); 

		tbSpBoSRSectoDAO salesDAO = new tbSpBoSRSectoDAO();
		Session session = salesDAO.getSession();
		
		Query req;
		String strReq = "";
		strReq = salesDAO.QUERY_GRAPH_DELTA_P_M;
		req = session.createSQLQuery(session.getNamedQuery(strReq).getQueryString());
		req.setInteger("cs_adm_pdt", cs_adm_pdt);
		req.setInteger("cs_adm_rgpt", cs_adm_rgpt);
		req.setInteger("cs_adm_prstn", cs_adm_prstn);
		req.setInteger("CS_SRC", CS_SRC);
		req.setInteger("cs_network", cs_network);
		req.setInteger("cs_month", cs_month);
		req.setString("nivGeog", nivGeog);
		req.setInteger("idGeog", idGeogInteger);
		
		req.setString("unite", unite);
		
		req.setString("data", unite + periode);	
		req.setString("quota", quota);
		
		ArrayList result = new ArrayList(req.list());
		Iterator iter = result.iterator();
		HashMap hm = new HashMap();
		ArrayList categ = new ArrayList();
		ArrayList data1 = new ArrayList();
		ArrayList data2 = new ArrayList();
		ArrayList data3 = new ArrayList();
		int i = 0;
		while (iter.hasNext()) {
			Object[] tmp = (Object[]) iter.next();
			
			Integer idGeogTmp = 0;
			String niveauTmp = "";
			if (tmp[0].toString().equals("0")) {
				niveauTmp = "France";
				idGeogTmp = Integer.parseInt(tmp[14].toString()); //data.getCsNetwork();
			} else if (tmp[0].toString().equals("1")) {
				niveauTmp = "Zone";
				idGeogTmp = Integer.parseInt(tmp[14].toString()); //data.getCsZone();
			} else if (tmp[0].toString().equals("2")) {
				niveauTmp = "Region";
				idGeogTmp = Integer.parseInt(tmp[14].toString()); //data.getCsRegion();
			} else if (tmp[0].toString().equals("3")) {
				niveauTmp = "Secteur";
				idGeogTmp = Integer.parseInt(tmp[14].toString()); //data.getCsSecteur();
			}  
			
			Double indicateurValue1 = UtilsSalesReport.getIndicateur(
										indicateur, 
										Double.parseDouble(tmp[2].toString()), 
										Double.parseDouble(tmp[3].toString()), 
										Double.parseDouble(tmp[4].toString()), 
										Double.parseDouble(tmp[5].toString()), 
										1);
			Double indicateurValue2 = UtilsSalesReport.getIndicateur(
										indicateur, 
										Double.parseDouble(tmp[6].toString()), 
										Double.parseDouble(tmp[7].toString()), 
										Double.parseDouble(tmp[8].toString()), 
										Double.parseDouble(tmp[9].toString()), 
										1);
			Double indicateurValue3 = UtilsSalesReport.getIndicateur(
										indicateur, 
										Double.parseDouble(tmp[10].toString()), 
										Double.parseDouble(tmp[11].toString()), 
										Double.parseDouble(tmp[12].toString()), 
										Double.parseDouble(tmp[13].toString()), 
										1);
			
			if (i == 0) {
				if (france.equals("true")) {
					categ.add(tmp[1]);
					data3.add("{y:" + indicateurValue1 + ", idGeog:" + idGeogTmp + ", niveau:'" + niveauTmp + "'}"); // Mensuel
					data1.add("{y:" + indicateurValue2 + ", idGeog:" + idGeogTmp + ", niveau:'" + niveauTmp + "'}"); // Cumul Fixe
					data2.add("{y:" + indicateurValue3 + ", idGeog:" + idGeogTmp + ", niveau:'" + niveauTmp + "'}"); // Cumul Cycle
				}
			} else if (i == 1) {
				if (zone.equals("true")) {
					categ.add(tmp[1]);
					data3.add("{y:" + indicateurValue1 + ", idGeog:" + idGeogTmp + ", niveau:'" + niveauTmp + "'}"); // Mensuel
					data1.add("{y:" + indicateurValue2 + ", idGeog:" + idGeogTmp + ", niveau:'" + niveauTmp + "'}"); // Cumul Fixe
					data2.add("{y:" + indicateurValue3 + ", idGeog:" + idGeogTmp + ", niveau:'" + niveauTmp + "'}"); // Cumul Cycle 
				}
			} else if (i == 2) {
				if (region.equals("true")) {
					categ.add(tmp[1]);
					data3.add("{y:" + indicateurValue1 + ", idGeog:" + idGeogTmp + ", niveau:'" + niveauTmp + "'}"); // Mensuel
					data1.add("{y:" + indicateurValue2 + ", idGeog:" + idGeogTmp + ", niveau:'" + niveauTmp + "'}"); // Cumul Fixe
					data2.add("{y:" + indicateurValue3 + ", idGeog:" + idGeogTmp + ", niveau:'" + niveauTmp + "'}"); // Cumul Cycle
				}
			} else {
				categ.add(tmp[1]);
				data3.add("{y:" + indicateurValue1 + ", idGeog:" + idGeogTmp + ", niveau:'" + niveauTmp + "'}"); // Mensuel
				data1.add("{y:" + indicateurValue2 + ", idGeog:" + idGeogTmp + ", niveau:'" + niveauTmp + "'}"); // Cumul Fixe
				data2.add("{y:" + indicateurValue3 + ", idGeog:" + idGeogTmp + ", niveau:'" + niveauTmp + "'}"); // Cumul Cycle
			}
			i++;
		}

		// Ajout du niveau UGA
		if (nivGeog.equals("Secteur") || nivGeog.equals("UGA")) {
			strReq = salesDAO.QUERY_GRAPH_DELTA_P_M_UGA;
			req = session.createSQLQuery(session.getNamedQuery(strReq).getQueryString());
			req.setInteger("cs_adm_pdt", cs_adm_pdt);
			req.setInteger("cs_adm_rgpt", cs_adm_rgpt);
			req.setInteger("cs_adm_prstn", cs_adm_prstn);
			req.setInteger("CS_SRC", CS_SRC);
			req.setInteger("cs_network", cs_network);
			req.setInteger("cs_month", cs_month);
			req.setString("nivGeog", nivGeog);
			req.setInteger("idGeog", idGeogInteger);
			
			req.setString("unite", unite);
			
			req.setString("data", unite + periode);	
			req.setString("quota", quota);
			
			// req.setMaxResults(10);
			
			result = new ArrayList(req.list());
			iter = result.iterator();
			while (iter.hasNext()) {
				Object[] tmp = (Object[]) iter.next();
				Integer idGeogTmp = Integer.parseInt(tmp[14].toString()); //data.getCsCommonUga();
				String niveauTmp = "UGA";
				categ.add(tmp[1]);
				
				Double indicateurValue1 = UtilsSalesReport.getIndicateur(
											indicateur, 
											Double.parseDouble(tmp[2].toString()), 
											Double.parseDouble(tmp[3].toString()), 
											Double.parseDouble(tmp[4].toString()), 
											Double.parseDouble(tmp[5].toString()), 
											1);
				Double indicateurValue2 = UtilsSalesReport.getIndicateur(
											indicateur, 
											Double.parseDouble(tmp[6].toString()), 
											Double.parseDouble(tmp[7].toString()), 
											Double.parseDouble(tmp[8].toString()), 
											Double.parseDouble(tmp[9].toString()), 
											1);
				Double indicateurValue3 = UtilsSalesReport.getIndicateur(
											indicateur, 
											Double.parseDouble(tmp[10].toString()), 
											Double.parseDouble(tmp[11].toString()), 
											Double.parseDouble(tmp[12].toString()), 
											Double.parseDouble(tmp[13].toString()), 
											1);
				
				data3.add("{y:" + indicateurValue1 + ", idGeog:" + idGeogTmp + ", niveau:'" + niveauTmp + "'}"); // Mensuel
				data1.add("{y:" + indicateurValue2 + ", idGeog:" + idGeogTmp + ", niveau:'" + niveauTmp + "'}"); // Cumul Fixe
				data2.add("{y:" + indicateurValue3 + ", idGeog:" + idGeogTmp + ", niveau:'" + niveauTmp + "'}"); // Cumul Cycle
			}
					
		}

		// Ajout du niveau UGP
		if (nivGeog.equals("UGA")) {
			strReq = salesDAO.QUERY_GRAPH_DELTA_P_M_UGP;
			req = session.createSQLQuery(session.getNamedQuery(strReq).getQueryString())
				.addEntity("A", tbSpBoSRSalesUgp.class)
				.addScalar("id", Hibernate.INTEGER)
				.addScalar("niveau", Hibernate.STRING);
			req.setInteger("cs_adm_pdt", cs_adm_pdt);
			req.setInteger("cs_adm_rgpt", cs_adm_rgpt);
			req.setInteger("cs_adm_prstn", cs_adm_prstn);
			req.setInteger("CS_SRC", CS_SRC);
			req.setString("nivGeog", nivGeog);
			req.setInteger("idGeog", idGeogInteger);
			
			// req.setMaxResults(10);
			
			result = new ArrayList(req.list());
			iter = result.iterator();
			while (iter.hasNext()) {
				Object[] tmp = (Object[]) iter.next();
				tbSpBoSRSalesUgp data = (tbSpBoSRSalesUgp) tmp[2];
				Integer idGeogTmp = data.getCsUgp();
				String niveauTmp = "UGP";
				categ.add(tmp[1]);
				
				// A l'UGP, on force l'indicateur DeltaPM 
				
				data3.add("{y:" + data.getDeltaPm(unite, "MM", 1) + ", idGeog:" + idGeogTmp + ", niveau:'" + niveauTmp + "'}"); // Mensuel
				data1.add("{y:" + data.getDeltaPm(unite, "CF", 1) + ", idGeog:" + idGeogTmp + ", niveau:'" + niveauTmp + "'}"); // Cumul Fixe
				data2.add("{y:" + data.getDeltaPm(unite, "CC", 1) + ", idGeog:" + idGeogTmp + ", niveau:'" + niveauTmp + "'}"); // Cumul Cycle
			}
					
		}

		hm.put("nbData", categ.size());
		
		hm.put("categ", categ);
		hm.put("data1", data1);
		hm.put("data2", data2);
		hm.put("data3", data3);
		
		JSONObject json = JSONObject.fromObject(hm);

		response.addHeader("Content-Type", "application/json");
        response.getOutputStream().write(json.toString().getBytes("UTF-8"));
        response.getOutputStream().flush();

		return null;
	}

}
